const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const CssMinimizerWebpackPlugin = require('css-minimizer-webpack-plugin')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')


module.exports = {
  mode: 'development',
  entry: {
    index: path.resolve(__dirname, './src/js/Index.js'),
    list: path.resolve(__dirname, './src/js/List.js'),
    detail: path.resolve(__dirname, './src/js/Detail.js'),
  },
  output: {
    path: path.resolve(__dirname, "dist"),
    filename: 'js/[name].[contenthash:8].js'
  },
  module: {
    rules: [
      {
        enforce: "pre",  //前置loader,最先执行
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'babel-loader',
        options: {
          presets: [
            '@babel/preset-env',
            // '@babel/plugin-transform-runtime'
          ],
          plugins:[
            '@babel/plugin-transform-runtime'
          ]
        }
      },
      {
        test: /\.tpl$/,
        loader: 'ejs-loader',
        options: {
          esModule: false
        }
      },
      {
        test: /\.scss$/,
        use: [
          MiniCssExtractPlugin.loader,
          'css-loader',
          {
            loader: 'postcss-loader',
            // options:{
            //   postcssOptions:{
            //     plugins:[
            //       'postcss-preset-env'
            //     ]
            //   }
            // }
          },
          'sass-loader'
        ]
      },
      {
        test: /\.(png|svg|jpg|jpeg|gif|icon)$/i,
        type: "asset/resource",
        generator: {
          filename: "img/[name].[hash:6][ext]"
        }
      }
    ]
  },
  plugins: [
    new HtmlWebpackPlugin({
      title: "小米手机官网",
      filename: 'index.html',
      template: path.resolve(__dirname, './src/index.html'),
      minify: {
        // pro 默认值
        collapseWhitespace: true,
        removeComments: true,
      },
      chunksSortMode: 'manual',
      chunks: ['index'],
      excludeChunks: ['node_modules'],
      hash: true
    }),
    new HtmlWebpackPlugin({
      title: "小米手机官网",
      filename: 'list.html',
      template: path.resolve(__dirname, './src/list.html'),
      minify: {
        // pro 默认值
        collapseWhitespace: true,
        removeComments: true,
      },
      chunksSortMode: 'manual',
      chunks: ['list'],
      excludeChunks: ['node_modules'],
      hash: true
    }),
    new HtmlWebpackPlugin({
      title: "小米手机官网",
      filename: 'detail.html',
      template: path.resolve(__dirname, './src/detail.html'),
      minify: {
        // pro 默认值
        collapseWhitespace: true,
        removeComments: true,
      },
      chunksSortMode: 'manual',
      chunks: ['detail'],
      excludeChunks: ['node_modules'],
      hash: true
    }),
    new MiniCssExtractPlugin({
      filename: 'css/[name].css'
    }),
    new CssMinimizerWebpackPlugin({
      // minimize: true
    }),
    // new CleanWebpackPlugin()
  ],
  devServer: {
    compress: true,
    port: 3333,
    hot: true,
    open: true
  }
}
